package main

import (
	"fmt"
	"goshp"
	//"strings"
	//"math"
	//"os"
)

func Uint32ToBytes(x uint32) []byte {

	//dt.numberOfRecords = uint32(s[4]) | (uint32(s[5]) << 8) | (uint32(s[6]) << 16) | (uint32(s[7]) << 24)

	var buf [4]byte
	buf[0] = byte(x >> 0)
	buf[1] = byte(x >> 8)
	buf[2] = byte(x >> 16)
	buf[3] = byte(x >> 24)

	fmt.Printf("byte:%v\n", buf)

	return buf[:]
}


func Uint64ToBytes(x uint64) []byte {

	//dt.numberOfRecords = uint32(s[4]) | (uint32(s[5]) << 8) | (uint32(s[6]) << 16) | (uint32(s[7]) << 24)

	var buf [8]byte
	buf[0] = byte(x >> 0)
	buf[1] = byte(x >> 8)
	buf[2] = byte(x >> 16)
	buf[3] = byte(x >> 24)
	buf[4] = byte(x >> 32)
	buf[5] = byte(x >> 40)
	buf[6] = byte(x >> 48)
	buf[7] = byte(x >> 56)

	fmt.Printf("byte:%v\n", buf)

	return buf[:]
}


func Uint64ToBytesBig(x uint64) []byte {

	//dt.numberOfRecords = uint32(s[4]) | (uint32(s[5]) << 8) | (uint32(s[6]) << 16) | (uint32(s[7]) << 24)

	var buf [8]byte
	buf[0] = byte(x >> 56)
	buf[1] = byte(x >> 48)
	buf[2] = byte(x >> 40)
	buf[3] = byte(x >> 32)
	buf[4] = byte(x >> 24)
	buf[5] = byte(x >> 16)
	buf[6] = byte(x >> 8)
	buf[7] = byte(x >> 0)

	fmt.Printf("byte:%v\n", buf)

	return buf[:]
}


func testNewFromFile() {

	//Uint32ToBytes(1)
	//Uint64ToBytes(1)
	//Uint64ToBytesBig(1) 

	dt, err := goshp.NewFromFile("/home/aydink/Downloads/shape/test.dbf", "iso-8859-9")
	
	if err != nil {	
		fmt.Println(err)
	} 

	for i := 0; i < len(dt.Fields()); i++ {
		fmt.Printf("field name:%v\n", dt.Fields()[i].FieldName())
		fmt.Printf("field type:%v\n", dt.Fields()[i].FieldType())
		fmt.Printf("field length:%v\n", dt.Fields()[i].FieldLength())
	}


		n := 0

		for i := 0; i < dt.NumberOfRecords(); i++ {
			n++
			
			for j := 0; j < len(dt.Fields()); j++ {			
				v := dt.FieldValue(i, j)			
				fmt.Printf("%v \t", v)
			}
			fmt.Printf("\n")		
		}

		fmt.Printf("Total:%v\n", n)
		fmt.Printf("Total:%v\n", dt.FieldValue(dt.NumberOfRecords()-1, 2))

}


func testNew() {
	dt := goshp.New("ISO-8859-9")
	fmt.Printf("number of records: %v\n", dt.NumberOfRecords())

	err := dt.AddNumberField("id", 11)
	if err != nil {
		fmt.Printf("Adding a new field failed. Reason:%s\n", err.String())
	}

	err = dt.AddTextField("name", 50)
	if err != nil {
		fmt.Printf("Adding a new field failed. Reason:%s\n", err.String())
	}

	err = dt.AddBooleanField("married")
	if err != nil {
		fmt.Printf("Adding a new field failed. Reason:%s\n", err.String())
	}

	err = dt.AddDateField("birth day")
	if err != nil {
		fmt.Printf("Adding a new field failed. Reason:%s\n", err.String())
	}

	row := dt.AddNewRecord()
	err = dt.SetFieldValueByName(row, "id", "1")
	if err != nil {
		fmt.Printf("Adding a new field failed. Reason:%s\n", err.String())
	}
	
	err = dt.SetFieldValueByName(row, "name", "Aydın Kılıç")
	if err != nil {
		fmt.Printf("Adding a new field failed. Reason:%s\n", err.String())
	}
	
	err = dt.SetFieldValueByName(row, "married", "T")
	if err != nil {
		fmt.Printf("Adding a new field failed. Reason:%s\n", err.String())
	}
	
	err = dt.SetFieldValueByName(row, "birth day", "19750831")
	if err != nil {
		fmt.Printf("Adding a new field failed. Reason:%s\n", err.String())
	}
	
	row = dt.AddNewRecord()
	dt.SetFieldValueByName(row, "id", "2")
	dt.SetFieldValueByName(row, "name", "Yağmur Sude Kılıç")
	dt.SetFieldValueByName(row, "married", "F")
	dt.SetFieldValueByName(row, "birth day", "20020207")
	fmt.Printf("number of records: %v\n", dt.NumberOfRecords())

	fmt.Printf("fields:%v\n", dt.Fields())

	for i := 0; i < len(dt.Fields()); i++ {
		fmt.Printf("field name:%v\n", dt.Fields()[i].FieldName())
		fmt.Printf("field type:%v\n", dt.Fields()[i].FieldType())
		fmt.Printf("field length:%v\n", dt.Fields()[i].FieldLength())
	}

	for i := 0; i < dt.NumberOfRecords(); i++ {

		v := dt.FieldValue(i, 1)
		fmt.Printf("value:%v\n", v)
	}
	
	dt.SaveFile("/home/aydink/Desktop/deneme.dbf")
}
/*

	for i := 0; i < dt.NumberOfRecords(); i++ {
		//v := dt.FieldValue(i, 1)
		//fmt.Printf("row %v: %v\n", n, dt.FieldValueByName(i, "name"))

		_, value := dt.FieldValueByName(i, "name")
		fmt.Printf("row %v: %v \n", i, value)
	}

	//fmt.Printf("%s\n", dt.GetNormalizedFieldName("ğüşiöçĞÜŞİÖÇ"))
	//fmt.Printf("%v\n", uint8(125>>8))
*/

func main() {
	testNewFromFile()
	//testNew()
}
